package cn.iocoder.yudao.framework.datasource.core;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;

public class DynamicDataSourceContext {

    /**
     * 显式设置数据源
     *
     * @param clazz：PaxAdminMerchantDS/PaxAdminSelfDS/PaxCoreDS
     */
    public static void set(Class<?> clazz) {
        final DS annotation = clazz.getAnnotation(DS.class);
        final String dbName = annotation.value();
        DynamicDataSourceContextHolder.push(dbName);
    }

    /**
     * 清理数据源设置，务必在finally中调用
     */
    public static void clear() {
        DynamicDataSourceContextHolder.clear();
    }

    // demo
    public static void main(String[] args) {
        try {
            //set(XXXDS.class);

            // 执行数据库操作
        } finally {
            clear();
        }
    }

}
